package com.yaj.myfactorysupport.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaj.myfactorysupport.dto.TSmtProcessDto;
import com.yaj.myfactorysupport.entity.TSmtProcess;
import com.yaj.myfactorysupport.entity.TSmtRoll;
import com.yaj.myfactorysupport.entity.TSmtUser;
import com.yaj.myfactorysupport.mapper.TSmtProcessMapper;
import com.yaj.myfactorysupport.mapper.TSmtRollMapper;
import com.yaj.myfactorysupport.mapper.TSmtUserMapper;
import com.yaj.myfactorysupport.service.TSmtProcessService;
import com.yaj.myfactorysupport.util.ErrorCodeEnum;
import com.yaj.myfactorysupport.util.RespBean;
import com.yaj.myfactorysupport.util.SucessCodeEnum;
import com.yaj.myfactorysupport.vo.TSmtProcessVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wei
 * @since 2021-06-01
 */
@Slf4j
@Service
public class TSmtProcessServiceImpl extends ServiceImpl<TSmtProcessMapper, TSmtProcess> implements TSmtProcessService {
    @Resource
    private TSmtProcessMapper tSmtProcessMapper;

    @Resource
    private TSmtUserMapper tSmtUserMapper;

    @Resource
    private TSmtRollMapper tSmtRollMapper;

    @Override
    public RespBean SelSMTProcess(String OrderNo, BigDecimal BrNo, String MounterID, String SMTSide) {
        QueryWrapper<TSmtProcess> qw = new QueryWrapper<>();
        qw.select("ORDER_NO,BR_NO,MOUNTER_ID,SMT_SIDE,MAX(SMT_PROCESS) AS SMT_PROCESS");
        qw.eq("ORDER_NO", OrderNo);
        qw.eq("BR_NO",BrNo);
        qw.eq("MOUNTER_ID",MounterID);
        qw.eq("SMT_SIDE",SMTSide);
        qw.groupBy("ORDER_NO,BR_NO,MOUNTER_ID,SMT_SIDE");

        TSmtProcess tSmtProcess = tSmtProcessMapper.selectOne(qw);

        TSmtProcessVo tSmtProcessVo = new TSmtProcessVo();
        if(ObjectUtil.isNull(tSmtProcess)){
            tSmtProcessVo.setOrderNo(OrderNo);
            tSmtProcessVo.setBrNo(BrNo);
            tSmtProcessVo.setMounterId(MounterID);
            tSmtProcessVo.setSmtSide(SMTSide);
            //尚未登录时设置SmtProcess为0
            tSmtProcessVo.setSmtProcess("0");
        }else {
            BeanUtil.copyProperties(tSmtProcess,tSmtProcessVo);
        }

        return RespBean.success(tSmtProcessVo);
    }

    @Override
    public RespBean RegSMTProcess(TSmtProcess tSmtProcess) {
        try {
            int result = tSmtProcessMapper.insert(tSmtProcess);

            if(result==1){
                return RespBean.success(SucessCodeEnum.插入成功.getMsg());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return RespBean.error(ErrorCodeEnum.插入记录失败.getCode(),ErrorCodeEnum.插入记录失败.getMsg());
    }

    @Override
    public RespBean DelSMTProcess(String OrderNo, BigDecimal BrNo, String MounterID, String SMTSide) {
        try {
            int result = tSmtProcessMapper.delete(new QueryWrapper<TSmtProcess>().lambda()
                    .eq(TSmtProcess::getOrderNo, OrderNo)
                    .eq(TSmtProcess::getBrNo, BrNo)
                    .eq(TSmtProcess::getMounterId, MounterID)
                    .eq(TSmtProcess::getSmtSide, SMTSide));

            if(result ==1){
                return RespBean.success(SucessCodeEnum.删除成功.getMsg());
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }

        return RespBean.error(ErrorCodeEnum.删除记录失败.getCode(),ErrorCodeEnum.删除记录失败.getMsg());
    }

    @Override
    public RespBean ResetSMTProcess(TSmtProcessDto tSmtProcessDto) {
        try {
            TSmtUser tSmtUser = tSmtUserMapper.selectOne(new QueryWrapper<TSmtUser>().lambda()
                                                        .eq(TSmtUser::getWorkerId, tSmtProcessDto.getWorkerId())
                                                        .eq(TSmtUser::getPassword, tSmtProcessDto.getPassword()));

            if(ObjectUtil.isNull(tSmtUser)){
                return RespBean.error(ErrorCodeEnum.管理员密码错误.getCode(),ErrorCodeEnum.管理员密码错误.getMsg());
            }

            tSmtProcessMapper.delete(new QueryWrapper<TSmtProcess>().lambda()
                    .eq(TSmtProcess::getOrderNo, tSmtProcessDto.getOrderNo())
                    .eq(TSmtProcess::getBrNo, tSmtProcessDto.getBrNo())
                    .eq(TSmtProcess::getMounterId, tSmtProcessDto.getMounterId())
                    .eq(TSmtProcess::getSmtSide, tSmtProcessDto.getSmtSide()));

            tSmtRollMapper.delete(new QueryWrapper<TSmtRoll>().lambda()
                    .eq(TSmtRoll::getOrderNo, tSmtProcessDto.getOrderNo())
                    .eq(TSmtRoll::getBrNo, tSmtProcessDto.getBrNo())
                    .eq(TSmtRoll::getMounterId, tSmtProcessDto.getMounterId())
                    .eq(TSmtRoll::getSmtSide, tSmtProcessDto.getSmtSide()));
        } catch (Exception e) {
            log.error(e.getMessage());
            return RespBean.error(ErrorCodeEnum.删除记录失败.getCode(),ErrorCodeEnum.删除记录失败.getMsg());
        }

        return RespBean.success(SucessCodeEnum.重置成功.getMsg());
    }
}
